package com.huaiwei.usmManager.modules.usm.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huaiwei.usmManager.common.utils.Constant;
import com.huaiwei.usmManager.common.utils.PageUtils;
import com.huaiwei.usmManager.common.utils.Query;
import com.huaiwei.usmManager.modules.usm.dao.SalesDao;
import com.huaiwei.usmManager.modules.usm.entity.GoodsEntity;
import com.huaiwei.usmManager.modules.usm.entity.SalesEntity;
import com.huaiwei.usmManager.modules.usm.entity.TypeEntity;
import com.huaiwei.usmManager.modules.usm.service.GoodsService;
import com.huaiwei.usmManager.modules.usm.service.SalesService;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class SalesServiceImpl extends ServiceImpl<SalesDao, SalesEntity> implements SalesService {


  @Autowired
  private GoodsService goodsService;

  /**
   * 分页查询和筛选销售记录
   *
   * @param params
   * @return
   */
  @Override
  public PageUtils queryPage(Map<String, Object> params) {
    String customer_name = (String) params.get("customerName");
    String sales_date = (String) params.get("salesDate");
    //System.out.println(sales_date);

    String beginDate = "";
    String endDate = "";
    if (StringUtils.isNotEmpty(sales_date)) {
      beginDate = sales_date.split(",")[0];
      endDate = sales_date.split(",")[1];
    }

    IPage<SalesEntity> page = this.page(new Query<SalesEntity>().getPage(params),
        new QueryWrapper<SalesEntity>()
            .like(StringUtils.isNotEmpty(customer_name), "customer_name", customer_name)
            .between(StringUtils.isNotEmpty(sales_date), "sales_date", beginDate, endDate)
            .orderByDesc("sales_id")
    );

    return new PageUtils(page);
  }

  @Override
  public List<GoodsEntity> select(int status) {
    return goodsService.list(new QueryWrapper<GoodsEntity>()
        .eq("status", Constant.ON));
  }

}
